/**
 * @param {number[]} nums
 * @param {number} target
 * @return {number}
 * 双指针
 */
var threeSumClosest = function (nums, target) {
    nums.sort((a, b) => {
        return a - b
    })

    let res = Infinity
    for (let i = 0; i < nums.length; i++) {
        let start = i + 1, end = nums.length - 1
        while (start < end) {
            let sum = nums[i] + nums[start] + nums[end]
            if (Math.abs(sum - target) < Math.abs(res - target)) {
                res = sum
            }

            if (sum < target) {
                start++
            } else if (sum > target) {
                end--
            } else {
                return sum
            }
        }
    }
    
    return res
};